package com.zs.test;

import com.zs.pojo.Person;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/6/9 上午9:28
 */
public class InvokeClassMethod {
    public static void main(String [] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        //获取Class对象的实例
        Class<?> clzz = Person.class;
        //通过Class对象创建Person实例
        Object person = clzz.newInstance();

        //获取Person类的方法
        Method getName = clzz.getDeclaredMethod("getName");
        System.out.println("调用getName后："+getName.invoke(person));
        Method setName = clzz.getDeclaredMethod("setName", String.class);
        System.out.println("调用setName后："+setName.invoke(person,"周双"));
        Method getAge = clzz.getDeclaredMethod("getAge");
        getAge.setAccessible(true);
        System.out.println("调用getAge后："+getAge.invoke(null));
        Method getAddress = clzz.getDeclaredMethod("getAddress");
        getAddress.setAccessible(true);
        System.out.println("调用getAddress后："+getAddress.invoke(person));
        Method silentMethod = clzz.getDeclaredMethod("silentMethod");
        silentMethod.setAccessible(true);
        System.out.println("调用silentMethod后："+silentMethod.invoke(person));
        Method toString = clzz.getDeclaredMethod("toString");
        System.out.println("调用toString方法后："+toString.invoke(person ));
    }
}
